<div class="layui-card-header layui-card">
    <span class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>代理管理</cite></a>
        <a><cite>代理列表</cite></a>
        <a><cite>代付通道</cite></a>
    </span>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">代付通道</li>
            </ul>
            <div class="layui-tab-content">
                <style>
                    .center {
                        width: 80%;
                    }
                </style>
                <table class="layui-hide center" id="cList"></table>
            </div>
        </div>
    </div>
</div>
    <script>
        layui.use(['table','util'], function(){
            var $ = layui.$
            ,admin = layui.admin
            ,table = layui.table
            ,element = layui.element
            ,form = layui.form;

            element.render('breadcrumb', 'breadcrumb');//渲染导航信息

            var router = layui.router();
            var agentId = router.search.agentId;

             //标题内容模板
            var tplTitle = function(d){
                return '<a class="layui-btn layui-btn-sm" lay-href="agent/agentpay_passage/config/agentId='+ d.agentId +'/agentpayPassageId='+ d.agentpayPassageId +'"><i class="layui-icon layui-icon-component"></i>配置</a>'
            };

            var tplStatus = function(d){

                if(!d.id){ //不包含ID 说明该代理商为设置过该代付通道， 不可点击更新状态按钮
                    return "未设置";
                }

                var checkedHtml = " updateId='"+d.id+"' ";
                if(d.status == 1) {
                    checkedHtml += " checked";
                }
                return '<input type="checkbox" lay-filter="statusSwitch" lay-skin="switch" '+checkedHtml+' lay-text="启用|停用">';
            };

            var tplFeeEvery = function(d){
                if((d.id == null) ) {
                    return "未设置";
                }else {

                    if(d.feeType == 1){ //费率
                        return "<span style='color: blue'>"+ d.feeRate + "%<span>";
                    }else if(d.feeType == 2){ //单笔费用
                        return "<span style='color: blue'>"+ d.feeEvery/100 + "元/笔<span>";

                    }else if(d.feeType == 3){ //费率 +费用
                        return "<span style='color: blue'>["+ d.feeRate + "%+"+d.feeEvery/100+"元/笔]<span>";
                    }
                }
            };

            table.render({
                elem: '#cList'
                ,url: layui.setter.baseUrl + '/agent_agentpay_passage/list'
                ,where: {agentId: agentId , access_token: layui.data(layui.setter.tableName).access_token}
                ,cols: [[
                     {field:'agentpayPassageId', title: '通道ID'}
                    ,{field:'passageName', title: '通道名称'}
                    ,{field:'feeEvery', title: '代付费用', templet: tplFeeEvery}
                    ,{field:'status', title: '状态',templet: tplStatus }
                    ,{field:'edit', width: 200, fixed: 'right', title: '操作', templet: tplTitle }
                ]]
                ,page: false
                ,skin: 'line'
            });

            form.on('switch(statusSwitch)', function(data){

                var updateId = $(data.elem).attr('updateId');
                var updateState = data.elem.checked ? "1" : "0";
                var loadingIndex = layer.load();
                admin.req({
                    type: "POST",
                    url: layui.setter.baseUrl + '/agent_agentpay_passage/update',
                    data: {id: updateId, status: updateState},
                    success: function(res){
                        if(res.code == 0) {
                            table.reload('tableReload');
                        }
                    },
                    complete:function(){
                        layer.close(loadingIndex);
                    }
                });
            });
        });

    </script>
